//
//  ViewController.swift
//  trypubuliu
//
//  Created by ning on 2018/1/25.
//  Copyright © 2018年 ning. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
     let layout : XWWaterFallLayout = XWWaterFallLayout()
    fileprivate lazy var collectionView : UICollectionView = {
        
        //通过自定义layout改变collectionView的布局
       
        let itemMargin : CGFloat = 10
        layout.minimumLineSpacing = itemMargin
        layout.minimumInteritemSpacing = itemMargin
        layout.sectionInset = UIEdgeInsets(top: 0, left: itemMargin, bottom: itemMargin, right: itemMargin)
        layout.dataSource = self
        
        let collectionView : UICollectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.dataSource = self
        return collectionView;
    }()
    
    var addresss = ["adqhedhqwiuerhqwirhquihriuqrhq","qiojreq","qhrqipehr9u1e90412u981hje192833r4134r13444r13r41","jiqejh","qjniphriqhwiuerhqiuhrqiuhriuqhrquihruqniawo[rfqiowjrioqjIOWRJqio[rwhjoqi0]RWHJOqjrwopqJRW0Oqjrw0-qjRW0QWORJQ0rq0wrjqoprjmqjoq]jorpqjwropqwjor","nqipjweiqj=wer[qwrq[rqior","qjoe[jqkowejkoqwkeqow[kjreoiqw","kq[keopqkep]]qkrqkropqkoqr"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupCollectionView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController {
    func setupCollectionView()  {
        self.view.backgroundColor = UIColor.white
        view.addSubview(collectionView)
        collectionView.register(UINib.init(nibName: "HomeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "HomeCollectionViewCell")
        collectionView.backgroundColor = UIColor.clear
    }
}
extension ViewController : UICollectionViewDataSource{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return addresss.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionViewCell", for: indexPath) as? HomeCollectionViewCell
        cell?.contentLabel.text = addresss[indexPath.row]
        cell?.contentLabelConstraintHeight.constant = addresss[indexPath.row].stringAutoFitHeight(labelWidth: layout.itemWidth,font:UIFont.systemFont(ofSize: 14))
        return cell ?? UICollectionViewCell()
    }
}

//MARK: - XWWaterFallLayoutDataSource数据源方法
extension ViewController : XWWaterFallLayoutDataSource {
    func numberOfCols(_waterFallLayout: XWWaterFallLayout) -> Int {
        return 2
    }
    func itemHeight(_waterFallLayout: XWWaterFallLayout, item: Int) -> CGFloat {
    return 80 + addresss[item].stringAutoFitHeight(labelWidth: _waterFallLayout.itemWidth,font:UIFont.systemFont(ofSize: 14)) + _waterFallLayout.itemWidth / 1.5
    }
}

extension String {
    func stringAutoFitHeight(labelWidth: CGFloat,font: UIFont) -> CGFloat {
    let newFram = self.boundingRect(with: CGSize(width:labelWidth,height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [
        NSAttributedStringKey.font : font
        ], context: nil)
        return newFram.height
    }
}
